package com.bwie.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.domain.*;
import com.bwie.common.utils.redis.service.RedisService;
import com.bwie.system.mapper.ShopMapper;
import com.bwie.system.service.ShopService;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @Author：作者姓名
 * @Package：com.bwie.system.service.impl
 * @Project：_yk
 * @name：ShopServiceimpl
 * @Date：2024/6/17 10:45
 */
@Service
@Log4j2
public class ShopServiceimpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public static final String PARENT_ID="0";


    @Override
    //@PostConstruct
    public List<Shop> ShopAll(Shop shop) {
        /*if(redisService.hasKey("shopAll")){
            log.info("从redis中取数据……");
            return redisService.getCacheList("shopAll");
        }*/
        List<Shop> shops = shopMapper.ShopAll(shop);
        redisService.setCacheList("shopAll",shops);
        return shops;
    }

    @Override
    //@PostConstruct
    public List<TypeInfoVo> listAll() {
        if(redisService.hasKey("typeInfo")){
            log.info("从redis中加载三级联动数据……");
            return redisService.getCacheList("typeInfo");
        }
        List<TypeInfo> typeInfos = shopMapper.FindList();
        List<TypeInfoVo> collect = typeInfos.stream().filter(item -> PARENT_ID.equals(item.getParentId()))
                .map(newItme -> {
                    TypeInfoVo typeInfoVo = new TypeInfoVo();
                    BeanUtils.copyProperties(newItme, typeInfoVo);
                    return typeInfoVo;
                }).map(typeInfoVo -> {
                    typeInfoVo.setChiledrenList(ParentList(typeInfoVo, typeInfos));
                    return typeInfoVo;
                }).collect(Collectors.toList());
        redisService.setCacheList("typeInfo",collect);
        return collect;
    }

    @Override
    public List<Type> getType() {
        return shopMapper.getType();
    }

    @Override
    public Integer getAdd(Shop shop) {
        Integer add = shopMapper.Add(shop);
        if(add>0){
            redisService.deleteObject("shopAll");
        }
        return add;
    }

    @Override
    public Integer updStats(String stats, String id) {
        return shopMapper.updStats(stats,id);
    }

    @Override
    public List<Shopping> ShoppingAll() {
        if(redisService.hasKey("shopping")){
            log.info("从redis的hash取数据");
            return redisService.getCacheMapValue("shopping","shopping");
        }
        List<Shopping> shopping = shopMapper.Shopping();
        redisService.setCacheMapValue("shopping","shopping",shopping);
        return shopping;
    }

    @Override
    public Integer getAdds(Shop shop) {
        Integer adds = shopMapper.Adds(shop);
        if(adds>0){
            redisService.deleteObject("shopping");
        }
        return adds;
    }

    @Override
    public Integer Del(String id) {
        Integer del = shopMapper.Del(id);
        if(del>0){
            redisService.deleteObject("shopping");
        }
        return del;
    }

    @Override
    @Transactional
    public Integer handleDelete(String price, String userName) {
        shopMapper.Upd(price,userName);
        return 1;
    }

    @Override
    @Transactional
    public Integer handleDeletes(String[] ids) {
        String userName="用户";
        for (String id : ids) {
            Oder oder = new Oder();
            oder.setPrice(122);
            oder.setUserName(userName);
            oder.setIndate(new Date());
            Shopping find = shopMapper.Find(id);
            BeanUtils.copyProperties(find,oder);
            shopMapper.handleAdd(oder);
            shopMapper.Del(id);
        }
        redisService.deleteObject("shopping");
        return 1;
    }

    @Override
    public List<Oder> getOder() {
        return shopMapper.getOder();
    }

    @Override
    public Integer handleDel() {
        rabbitTemplate.convertAndSend("pxin", 1,message -> {
            message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
            return message;
        });
        return 1;
    }

    private List<TypeInfoVo> ParentList(TypeInfoVo typeInfoVo, List<TypeInfo> typeInfos) {
        String catId = typeInfoVo.getCatId();
        List<TypeInfoVo> collect = typeInfos.stream().filter(item -> catId.equals(item.getParentId()))
                .map(newItem -> {
                    TypeInfoVo typeInfoVo1 = new TypeInfoVo();
                    BeanUtils.copyProperties(newItem, typeInfoVo1);
                    typeInfoVo1.setChiledrenList(ParentList(typeInfoVo1, typeInfos));
                    return typeInfoVo1;
                }).collect(Collectors.toList());
        return collect;
    }
}
